﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestLib
{
	/// <summary>
	/// Summary description for UnitTest1
	/// </summary>
	[TestClass]
	public class TestServerLib
	{
		public TestServerLib()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region data members
		ServerNodeLib.PersistentSchedulerServer scheduler;
		#endregion

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public void MyClassInitialize(TestContext testContext)
		{
			scheduler = new ServerNodeLib.PersistentSchedulerServer();
			scheduler.Start();
		}
		//
		// Use ClassCleanup to run code after all tests in a class have run
		[ClassCleanup()]
		public void MyClassCleanup()
		{
			scheduler.Stop();
			scheduler = null;
		}
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[TestMethod()]
		public void TestAnnounce()
		{
			ServerNodeLib.NodeStatus myStatus = new ServerNodeLib.NodeStatus();
			myStatus.State = ServerNodeLib.NodeState.Idle;
			myStatus.NodeName = System.IO.Path.GetRandomFileName();
			myStatus.NodeId = scheduler.AnnounceMyself(myStatus.NodeName);
			Assert.IsTrue(myStatus.NodeId.Equals(Guid.Empty) == false);

			ServerNodeLib.ActionRequest ack = scheduler.SetStatus(myStatus.NodeId, myStatus);
			Assert.IsTrue(ack.ActionType == ServerNodeLib.ActionRequestType.NoAction);
			Assert.IsTrue(ack.PayloadKey == String.Empty);
			Assert.IsTrue(ack.PayloadValue == String.Empty);

			ServerNodeLib.NodeStatus otherStatus = scheduler.GetStatus(myStatus.NodeId);
			Assert.IsTrue(otherStatus.NodeId.Equals(myStatus.NodeId));
			Assert.IsTrue(otherStatus.NodeName.Equals(myStatus.NodeName));
			Assert.IsTrue(otherStatus.State.Equals(myStatus.State));

			ServerNodeLib.BatchTemplate bt = new ServerNodeLib.BatchTemplate();
			bt.BaseBatch = new ServerNodeLib.Batch();
			bt.EndConfigIndex = 10;
			bt.StartConfigIndex = 0;
			bt.Name = System.IO.Path.GetRandomFileName();

			bt.BaseBatch.BatchGroup = Guid.NewGuid();
			bt.BaseBatch.BatchId = Guid.NewGuid();
			bt.BaseBatch.Comments = "My comments %i%";
			bt.BaseBatch.LogFilenameFormat = "Log %i%.txt";
			bt.BaseBatch.Status = ServerNodeLib.BatchStatus.NotStarted;
			bt.BaseBatch.BatchCommands = new List<ServerNodeLib.BatchCommand>();
			ServerNodeLib.BatchCommand notepadCmd = new ServerNodeLib.BatchCommand();
			notepadCmd.Command = "notepad";
			//notepadCmd.
			//bt.BaseBatch.BatchCommands.Add();

			//scheduler.AddBatchTemplate(
		}
	}
}
